package com.allwees.bs.module.mms.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.allwees.bs.module.mms.entity.RetailerApply;
import com.allwees.core.common.constant.EStatus;
import com.allwees.core.dao.mybatis.dao.BaseDao;
import org.apache.commons.lang3.StringUtils;

/**
 * @author mouhaining
 * @since 2019-12-13
 */
public interface RetailerApplyDao extends BaseDao<RetailerApply> {
    /**
     * 根据phone、username查询
     * @return
     */
    default Integer selectCountByEmailWithRejected(String email) {
        QueryWrapper<RetailerApply> wrapper = new QueryWrapper<>();
        wrapper.notIn("status", EStatus.MMS_FIRST_AUDIT_REJECTED.value(), EStatus.MMS_FINAL_AUDIT_REJECTED.value())
                .and(w -> {
                    w.eq(StringUtils.isNotBlank(email), "email", email);
//                            .or()
//                            .eq(StringUtils.isNotBlank(mobile), "mobile", mobile);
                });
        return selectCount(wrapper);
    }

    /**
     * @return
     */
    default Integer selectCountByStoreNameWithApproved(String storeName) {
        QueryWrapper<RetailerApply> wrapper = new QueryWrapper<>();
        wrapper.notIn("status", EStatus.MMS_FIRST_AUDIT_REJECTED.value(), EStatus.MMS_FINAL_AUDIT_REJECTED.value())
                .eq("store_name", storeName);
        return selectCount(wrapper);
    }

    /**
     * @param companyName
     * @return
     */
    default Integer selectCountByCompanyNameWithRejected(String companyName) {
        QueryWrapper<RetailerApply> wrapper = new QueryWrapper<>();
        wrapper.eq("company_name", companyName)
                .notIn("status", EStatus.MMS_FIRST_AUDIT_REJECTED.value(), EStatus.MMS_FINAL_AUDIT_REJECTED.value());
        return selectCount(wrapper);
    }

    /**
     * 根据手机号查询
     *
     * @param phone
     * @return
     */
    default RetailerApply selectByPhone(String phone) {
        return findOneByField("mobile", phone);
    }

    default RetailerApply findByEmail(String email){
        if(StringUtils.isBlank(email)){
            return null;
        }
        QueryWrapper<RetailerApply> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(email),"email",email);
//                .or()
//                .eq(StringUtils.isNotBlank(email),"mobile",mobile);
        return selectOne(wrapper);
    }
}
